<?php
/*
if ( 'POST' != $_SERVER['REQUEST_METHOD'] ) {
	header('Allow: POST');
	header('HTTP/1.1 405 Method Not Allowed');
	header('Content-Type: text/plain');
	exit;
}*/

/** Sets up the WordPress Environment. */
require( dirname(__FILE__) . '/wp-load.php' );

nocache_headers();

$act = (isset($_REQUEST['act'])) ? trim($_REQUEST['act']) : "register";
if ($act == 'city') {
	$ret = "<option value='0'>&nbsp;&nbsp;</option>";
	$id = intval($_REQUEST['id']);
	$sql = $wpdb->prepare("SELECT region_id, region_name FROM wp_region WHERE parent_id = %d", $id);
	$cities = $wpdb->get_results($sql);
	foreach($cities as $city) {
		$ret .= "<option value='" . $city->region_id . "'>" . $city->region_name . "</option>";
	}
	echo $ret;
	exit(0);
} else {
	$error = false;
	$error_msg = '';
	$sn = ''; //参选号
	$card_sn = ''; //充值卡卡号

	$name = (isset($_POST['name'])) ? trim(strip_tags($_POST['name'])) : null;
	$eng_name = (isset($_POST['eng_name'])) ? trim(strip_tags($_POST['eng_name'])) : null;
	$sex = strip_tags($_POST['sex']);
	$birthday = strip_tags($_POST['birthday']);
	$nation = (isset($_POST['nation'])) ? trim(strip_tags($_POST['nation'])) : null;
	$region_id = (isset($_POST['city'])) ? intval($_POST['city']) : -1;
	$school = (isset($_POST['school'])) ? trim(strip_tags($_POST['school'])) : null;
	$post = (isset($_POST['post'])) ? trim(strip_tags($_POST['post'])) : null;
	$postcode = (isset($_POST['postcode'])) ? trim(strip_tags($_POST['postcode'])) : null;
	$parent = (isset($_POST['parent'])) ? trim(strip_tags($_POST['parent'])) : null;
	$phone = (isset($_POST['phone'])) ? trim(strip_tags($_POST['phone'])) : null;
	$idcard = (isset($_POST['idcard'])) ? trim(strip_tags($_POST['idcard'])) : null;
	$email = (isset($_POST['email'])) ? trim(strip_tags($_POST['email'])) : null;
	$teacher = (isset($_POST['teacher'])) ? trim(strip_tags($_POST['teacher'])) : null;
	$hobby = (isset($_POST['hobby'])) ? trim(strip_tags($_POST['hobby'])) : null;
	$group_id = (isset($_POST['group_id'])) ? trim(strip_tags($_POST['group_id'])) : null;

	$player_info = array('name' => $name, 'eng_name' => $eng_name, 'sex' => $sex, 'birthday' => strtotime($birthday),
                     'nation' => $nation, 'region_id' => $region_id, 'school' => $school,
                     'class' => '', 'post' => $post, 'postcode' => $postcode, 'parent_name' => $parent,
                     'phone' => $phone, 'idcard' => $idcard, 'email' => $email, 'teacher' => $teacher,
                     'hobby' => $hobby, 'group_id' => $group_id, 'reg_time' => time() );
	$res = $wpdb->get_row($wpdb->prepare("SELECT * FROM wp_player WHERE idcard = %s", $player_info['idcard']));
	if ($res) {
		$error = true;
		$error_msg = "身份证号码重复，请检查是否已经报过名。\n";
	} else {
		$ins_flag = $wpdb->insert("wp_player", $player_info);
		if (!$ins_flag) {
			$error = true;
			$error .= "有可能填写的资料有问题，请重新填写！\n";
		} else {
			$res = $wpdb->get_row($wpdb->prepare("SELECT * FROM wp_player WHERE idcard = %s", $player_info['idcard']));
			$player_id = $res->ID;
			$city = $wpdb->get_row($wpdb->prepare("SELECT * FROM wp_region WHERE region_id = %d", $region_id));
			if ($city) {
				$province = $wpdb->get_row($wpdb->prepare("SELECT * FROM wp_region WHERE region_id = %d", $city->parent_id));
			} else {
				$province = null;
			}
			$prefix = '';
			if ($province) {
				$p = $province->region_name;
				for($i = 0; $i < mb_strlen($p); $i++) {
					$prefix .= getfirstchar(mb_substr($p, $i, 1, "UTF-8"));
				}
				$prefix .= '-';
				$c = $city->region_name;
				for($i = 0; $i < mb_strlen($c); $i++) {
					$prefix .= getfirstchar(mb_substr($c, $i, 1, "UTF-8"));
				}
			} else {
				$prefix = "HONGEN";
			}
			$prefix .= '-';
			$rnd = rand(0, 99);
			$sn = $prefix . sprintf("%06d%02d", $player_id, $rnd);

			$wpdb->query($wpdb->prepare("UPDATE wp_player SET player_sn = %s WHERE ID = %d", $sn, $player_id));
			//处理赠送洪恩宝宝益智乐园充值卡
			$card = $wpdb->get_row("SELECT * FROM wp_kid_card WHERE player_id = 0");
			if ($card) {
				$wpdb->query($wpdb->prepare("UPDATE wp_kid_card SET player_id = %d WHERE kid_card_id = %d", $player_id, $card->kid_card_id));
				$card_sn = $card->card_sn;
			}
		}
	}
	get_header();
	echo '<div class="content">';
	echo '<div class="reg_wrapper2">';
	if (!$error) {
		echo '<p class="msg">';
		echo '恭喜你，你已经成功报名了！<br />你的参选号是: ' . $sn;
		echo '<br />各地区活动时间详见<a href="' . get_bloginfo('url') . '/?cat=3" target="_blank">分选区介绍</a>';
		$mail_subject = '恭喜你成功报名！';
		$mail_msg = "恭喜你，你已经成功报名了！\n你的参选号是: $sn";
		if ($card_sn != '') {
			echo '<br />我们赠送一张洪恩宝宝益智乐园的充值卡，<br />价值30元，卡号是：' . $card_sn;
			echo '<br />洪恩宝宝益智乐园：<a href="http://kid.hongen.com" target="_blank">http://kid.hongen.com</a>';
			$mail_msg .= "\n我们赠送一张洪恩宝宝益智乐园的充值卡，价值30元，卡号是：$card_sn";
			$mail_msg .= "\n洪恩宝宝益智乐园：http://kid.hongen.com";
		}
		echo '<br />请记录以上信息!';
		$res = wp_mail($email, $mail_subject, $mail_msg);
		echo '<br />我们已将以上信息发送到你的邮箱 ' . $email . ' 中。';
		echo '<br /><a href="' . "<?php bloginfo('stylesheet_directory'); ?>/2010_baoming.doc" . '">下载报名表</a>';
		echo '</p>';
	} else {
		echo '<p class="msg">很遗憾，报名未成功！<br />' . $error_msg . '</p>';
	}
	echo '</div>';
	echo '</div><!-- content -->';
	get_footer();
}

function getfirstchar($s0){
	$fchar=ord($s0{0});
	if ($fchar >= ord("a") and $fchar <= ord("Z") ) return strtoupper($s0{0});
	$s = iconv("UTF-8", "GB2312", $s0);
	$asc = ord($s{0}) * 256 + ord($s{1}) - 65536;
	if($asc>=-20319 and $asc<=-20284) return "A";
	if($asc>=-20283 and $asc<=-19776) return "B";
	if($asc>=-19775 and $asc<=-19219) return "C";
	if($asc>=-19218 and $asc<=-18711) return "D";
	if($asc>=-18710 and $asc<=-18527) return "E";
	if($asc>=-18526 and $asc<=-18240) return "F";
	if($asc>=-18239 and $asc<=-17923) return "G";
	if($asc>=-17922 and $asc<=-17418) return "H";              
	if($asc>=-17417 and $asc<=-16475) return "J";              
	if($asc>=-16474 and $asc<=-16213) return "K";              
	if($asc>=-16212 and $asc<=-15641) return "L";              
	if($asc>=-15640 and $asc<=-15166) return "M";              
	if($asc>=-15165 and $asc<=-14923) return "N";              
	if($asc>=-14922 and $asc<=-14915) return "O";              
	if($asc>=-14914 and $asc<=-14631) return "P";              
	if($asc>=-14630 and $asc<=-14150) return "Q";              
	if($asc>=-14149 and $asc<=-14091) return "R";              
	if($asc>=-14090 and $asc<=-13319) return "S";              
	if($asc>=-13318 and $asc<=-12839) return "T";              
	if($asc>=-12838 and $asc<=-12557) return "W";              
	if($asc>=-12556 and $asc<=-11848) return "X";              
	if($asc>=-11847 and $asc<=-11056) return "Y";              
	if($asc>=-11055 and $asc<=-10247) return "Z";  
	return null;
}
?>
